// pages/classify/index.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        dictValue: '',
        addShow: false,
        updateObj: {},
        type: 1,
        status: '',
        list: [
            { name: '预付归类', status: 1, classifyList: [] },
            { name: '耗损归类', status: 4, classifyList: [] },
            { name: '支出归类', status: 5, classifyList: [] },
            { name: '借款归类', status: 2, classifyList: [] },
            { name: '挂账归类', status: 3, classifyList: [] },
            
        ],
        activeNames: [],
        admin: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getList();
        wx.hideShareMenu();
        this.setData({ 
          admin: wx.getStorageSync('admin')
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getList(1);
        this.getList(2);
        this.getList(3);
        this.getList(4);
        this.getList(5);
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    getList(status) {
        wx.showLoading();
        wx.request({
            url: `${app.globalData.baseUrl}/api/classify/selDictData?status=${status}`,
            method: "GET",
            header: {
                'token': wx.getStorageSync('token'),
                "Content-Type": "application/json"
            },
            success: (res) => {
                wx.hideLoading();
                if (res.data.code == 0) {
                    const arr = res.data.data;
                    arr.forEach((item, index) => {
                        item.index = arr?.length - index;
                    });
                    const list = JSON.parse(JSON.stringify(this.data.list));
                    list.forEach(item => {
                        if (item.status == status) {
                            item.classifyList = arr;
                        }
                    })
                    this.setData({ list });
                } else {
                    wx.showToast({
                        title: res.data.msg || '出错啦！',
                        icon: 'error',
                        duration: 2000
                    })
                }
            }
        })
    },

    add(e) {
        this.setData({ addShow: true, type: 1, status: e.currentTarget.dataset.status });
    },
    update(e) {
        this.setData({
            addShow: true,
            type: 2,
            updateObj: e.currentTarget.dataset,
            dictValue: e.currentTarget.dataset.dictvalue,
            status: e.currentTarget.dataset.status
        });
    },
    delete(e) {
        const id = e.currentTarget.dataset.code;
        const status = e.currentTarget.dataset.status;
        const that = this;
        wx.showModal({
            content: '确认删除吗？',
            success: function (sm) {
                if (sm.confirm) {
                    wx.request({
                        url: `${app.globalData.baseUrl}/api/classify/delDictData?id=${id}&status=${status}`,
                        method: "GET",
                        header: {
                            'token': wx.getStorageSync('token'),
                            "Content-Type": "application/json"
                        },
                        success: (res) => {
                            if (res.data.code == 0) {
                                that.getList(status);
                                wx.showToast({
                                    title: '成功删除',
                                });
                            } else {
                                wx.showToast({
                                    title: res.data.msg || '报错啦！',
                                    duration: 2000
                                })
                            }
                        }
                    })
                } else {
                    console.log('用户点击取消');
                }
            }
        })
    },
    onDictValueChange(e) {
        this.setData({ dictValue: e.detail });
    },
    onDictValueHidden() {
        this.setData({ addShow: false, dictValue: '', status: '' });
    },
    addDictValue() {
        wx.showLoading();
        if (!this.data.dictValue) {
            wx.showToast({
                title: `请填写归类名称`,
                icon: 'error',
            })
            return;
        }
        const data = { dictValue: this.data.dictValue, status: this.data.status }
        if (this.data.type == 2) data.dictCode = this.data.updateObj.dictcode;
        wx.request({
            url: `${app.globalData.baseUrl}${this.data.type == 1 ? '/api/classify/saveDictData' : '/api/classify/updateDictData'}`,
            method: "POST",
            data: data,
            header: {
                "Content-Type": "application/json",
                'token': wx.getStorageSync('token'),
            },
            success: (res) => {
                wx.hideLoading();
                if (res.data.code == 0) {
                    wx.showToast({
                        title: '操作成功',
                    });
                    this.getList(this.data.status);
                    this.onDictValueHidden();
                } else if (res.data.code == 500) {
                    wx.showToast({
                        title: res.data.msg || '报错啦！',
                        icon: 'error',
                        duration: 2000
                    })
                } else {
                    wx.showToast({
                        title: '系统错误，请重新登录',
                    })
                }
            },
            fail: () => {
                wx.hideLoading();
                wx.showToast({
                    title: '系统错误',
                });
                _that.setData({ loading: false });
            }
        })
    },
    collapseChange(e) {
        this.setData({ activeNames: e.detail.slice(-1) })
      },
})